//
//  WBVisitorView.swift
//  Swift_Weibo
//
//  Created by Tim on 2019/8/13.
//  Copyright © 2019 Tim. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {
    // MARK: - 公共属性
    /// 注册按钮
    lazy var registerBtn : UIButton = UIButton.textBtn(
        title: "注册",
        normalColor: UIColor.orange,
        highlightedColor: UIColor.black,
        backgroundImg: "common_button_white_disable")
    
    lazy var loginBtn : UIButton = UIButton.textBtn(
        title: "登录",
        normalColor: UIColor.darkGray,
        highlightedColor: UIColor.black,
        backgroundImg: "common_button_white_disable")
    
    
    /// 重写 didSet 方法，完成赋值操作
    var visitorInfo : [String : String]? {
        didSet {
            guard let message = visitorInfo?["message"],
                let imgName = visitorInfo?["imgName"]
            else {
                return
            }
            
            if imgName == "" {
                startAnimation()
                return
            }
            
            iconView.image = UIImage(named: imgName)
            houseView.isHidden = true
            maskIconView.isHidden = true
            tipLabel.text = message
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 私有控件
    /// iconView
    private lazy var iconView      = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var houseView     = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var maskIconView  = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var tipLabel      = UILabel.textLabel(text: "关注一些人，回这里看看有什么惊喜", fontSize: 14.0, textColor: UIColor.darkGray)
}

extension WBVisitorView {
    private func setupUI() {
        backgroundColor = UIColor(hex: 0xededed)
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseView)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        // 标签文字居中
        tipLabel.textAlignment = .center
        
        // 使用自动布局需要关闭 autoresizing
        for subview in subviews {
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        
        let margin : CGFloat = 20.0
        // MARK: - 添加约束
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        
        houseView.snp.makeConstraints { (make) in
            make.center.equalTo(iconView)
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView.snp_bottom).offset(margin)
            make.centerX.equalTo(iconView)
            make.width.equalTo(236)
        }
        
        registerBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp_bottom).offset(margin)
            make.left.equalTo(tipLabel)
            make.width.equalTo(100)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.top.width.equalTo(registerBtn)
            make.right.equalTo(tipLabel)
        }
        
        maskIconView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(registerBtn.snp_bottom)
        }
    }
    
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * Double.pi
        animation.repeatCount = MAXFLOAT
        animation.duration = 15
        
        // 配置动画完成不删除
        animation.isRemovedOnCompletion = false
        iconView.layer.add(animation, forKey: nil)
    }
}
